﻿using AutoMapper;
using B.S.Outsourcing.Domain;
using B.S.Outsourcing.ErrorCode;
using B.S.Outsourcing.Infatructrue;
using B.S.Outsourcing.Read.Api.Application.Command.OutsourcingArrivalOrderModel;
using MediatR;

namespace B.S.Outsourcing.Read.Api.Application.CommandHandler.OutsourcingArrivalOrderModel
{
    public class QueryArrivaIDCommandHandler : IRequestHandler<QueryArrivaIDCommand, ApiPaging<OutsourcingArrivalOrder>>
    {
        private readonly IBaseRepository<OutsourcingArrivalOrder> baseRepository;
        private readonly IMapper mapper;
        private readonly DBHelper1 dbHelper;

        public QueryArrivaIDCommandHandler(IBaseRepository<OutsourcingArrivalOrder> baseRepository, IMapper mapper, DBHelper1 dbHelper)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
            this.dbHelper = dbHelper;
        }

        public async Task<ApiPaging<OutsourcingArrivalOrder>> Handle(QueryArrivaIDCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<OutsourcingArrivalOrder> result = new ApiPaging<OutsourcingArrivalOrder>();
            var sql = from a in baseRepository.GetAll()
                      where request.Id.Contains(a.Id)
                      select new OutsourcingArrivalOrder
                      {
                          Id = a.Id,
                          DocumentCode=a.DocumentCode,
                          BusinessType = a.BusinessType,
                          OutsourcingOrderNumber = a.OutsourcingOrderNumber,
                          MaterialCode = a.MaterialCode,
                          Remark = a.Remark,
                          DocumentDate = a.DocumentDate,
                          Operator = a.Operator,
                          Supplier = a.Supplier,
                          MaterialName = a.MaterialName,
                          DocumentStatus = a.DocumentStatus,
                          Auditor = a.Auditor,
                          PlanOrderNumber = a.PlanOrderNumber,
                          ArrivalQuantity = a.ArrivalQuantity,
                          BusinessStatus = a.BusinessStatus,
                          AuditDate = a.AuditDate,
                          FollowOrderNumber = a.FollowOrderNumber,
                          Warehouse = a.Warehouse,
                          CreatedAt = a.CreatedAt,
                          CreatedBy = a.CreatedBy,
                          LastModifiedAt = a.LastModifiedAt,
                          LastModifiedBy = a.LastModifiedBy,
                          IsDelete = a.IsDelete,
                          OrderDeliveryDate = a.OrderDeliveryDate,
                      };
            result.Code = ApiEnums.成功;
            result.Message = "成功";
            result.list = sql.ToList();
            return await Task.FromResult(result);
        }
    }
}
